local assets = {
	Asset("ANIM", "anim/musha_armor_princess.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_armor_princess.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_armor_princess.tex"),
	Asset("ANIM", "anim/musha_backpack.zip")
}

local function heal_owner(inst, owner)
	if owner.components.hunger and owner.components.hunger:GetPercent() > 0.3 then
		if owner.components.health and owner.components.health:GetPercentWithPenalty() < 1 then
			owner.components.health:DoDelta(1, true)
		elseif owner.components.sanity and owner.components.sanity:GetPercentWithPenalty() < 0.8 then
			owner.components.sanity:DoDelta(1.5, true)
		else
			return
		end
		owner.components.hunger:DoDelta(-1.5, true)
	end
end

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.armor:SetAbsorption(0)
		return
	end
	inst.components.armor:SetAbsorption(0.32 + inst.components.musha_equipment.currentlevel * 0.02)
	if display then
		inst.components.talker:Say(
			string.format(
				STRINGS.MUSHA_ARMOR_PRINCESS ..
					" (LV%d)\n" ..
						STRINGS.MUSHA_ARMOR ..
							" (%d)" ..
								STRINGS.MUSHA_ITEM_FREEZE ..
									"(%d)\n" .. STRINGS.MUSHA_ITEM_REGEN .. "\n[" .. STRINGS.MUSHA_ITEM_WARMNCOOL .. "]",
				inst.components.musha_equipment.currentlevel,
				32 + inst.components.musha_equipment.currentlevel * 2,
				math.ceil(inst.components.musha_equipment.currentlevel * 0.1) * 10 + 10
			)
		)
	end
end

local function ChangeInsulation(inst)
	if TheWorld.state.issummer then
		inst.components.insulator:SetSummer()
	elseif not TheWorld.state.issummer then
		inst.components.insulator:SetWinter()
	end
end

------
local function OnBlocked(owner)
	owner.SoundEmitter:PlaySound("dontstarve/wilson/hit_scalemail")
end

local function onopen(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/lighter_on")
end

local function onclose(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/lighter_on")
end

local function OnEquip(inst, owner)
	inst.heal_task =
		inst:DoPeriodicTask(
		1,
		function()
			heal_owner(inst, owner)
		end
	)

	inst:ListenForEvent("blocked", OnBlocked, owner)

	if TheWorld.state.issummer then
		inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")
	elseif not TheWorld.state.issummer then
		inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
	end

	Updata(inst)

	if inst.components.container ~= nil then
		inst.components.container:Open(owner)
	end

	ChangeInsulation(inst)
	inst:WatchWorldState("season", ChangeInsulation)

	owner.AnimState:OverrideSymbol("swap_body", "musha_armor_princess", "swap_body")

	local switch = {
		["BMM"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack6", "swap_body_tall")]],
		["BT"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack4", "swap_body_tall")]],
		["BS"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack3", "swap_body_tall")]],
		["BM"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack", "swap_body_tall")]],
		["BL"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack2", "swap_body_tall")]],
		["WSP"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_pink", "swap_body_tall")]],
		["WSR"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_danger", "swap_body_tall")]],
		["WSB"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_safe", "swap_body_tall")]],
		["WSH"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest", "swap_body_tall")]],
		["WLR"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "armor_butterfly", "swap_body_tall")]]
	}

	if inst.visual ~= "off" then
		loadstring(switch[string.upper(inst.visual)])
	end

	inst.freezefn = function(_, data)
		local attacker = data.attacker
		if data and attacker and attacker.components.freezable and attacker.components.combat then
			local fx = SpawnPrefab("icespike_fx_" .. math.random(4))
			local freeze_value = math.ceil(inst.components.musha_equipment.currentlevel * 0.1) * 0.1 + 0.1
			if math.random() < freeze_value then
				attacker.components.freezable:AddColdness(freeze_value)
				attacker.components.freezable:SpawnShatterFX()
				fx.Transform:SetScale(1, math.ceil(inst.components.musha_equipment.currentlevel * 0.1) * 0.25 + 0.5, 1)
				fx.Transform:SetPosition(attacker:GetPosition():Get())
			end
		end
	end

	inst:ListenForEvent("attacked", inst.freezefn, owner)
end

local function OnUnequip(inst, owner)
	inst:RemoveEventCallback("blocked", OnBlocked, owner)
	owner.AnimState:ClearOverrideSymbol("swap_body")
	owner.AnimState:ClearOverrideSymbol("backpack")
	owner.AnimState:ClearOverrideSymbol("swap_body_tall")

	if inst.components.container ~= nil then
		inst.components.container:Close(owner)
	end

	if inst.heal_task then
		inst.heal_task:Cancel()
		inst.heal_task = nil
	end

	inst:RemoveEventCallback("attacked", inst.freezefn, owner)
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	inst.AnimState:SetBank("armor_wood")
	inst.AnimState:SetBuild("musha_armor_princess")
	inst.AnimState:PlayAnimation("anim")

	inst:AddTag("backpack")
	inst:AddTag("aquatic")
	inst:AddTag("ruins")
	inst:AddTag("metal")
	inst:AddTag("musha_items")

	inst.foleysound = "dontstarve/movement/foley/metalarmour"

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		inst.OnEntityReplicated = function(inst)
			inst.replica.container:WidgetSetup("krampus_sack")
		end
		return inst
	end

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:InitMushaEquip("armor", 450, 0)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			if inst.components.armor.absorb_percent ~= 0 then
				inst.components.musha_equipment:StopConsuming()
				inst.components.armor:SetAbsorption(0)
				inst.components.talker:Say(
					STRINGS.MUSHA_ARMOR_BROKEN .. "\n" .. STRINGS.MUSHA_ARMOR .. " (0)\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)"
				)
			end
			inst.components.musha_equipment:StopConsuming()
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_armor_princess.xml"

	inst:AddComponent("insulator")
	inst.components.insulator:SetInsulation(TUNING.INSULATION_LARGE)

	inst:AddComponent("container")
	inst.components.container:WidgetSetup("krampus_sack")
	inst.components.container.onopenfn = onopen
	inst.components.container.onclosefn = onclose

	inst.type = "fire"

	return inst
end

return Prefab("musha_armor_princess", fn, assets)
